package com.system.user.config;

import com.system.user.entity.PayEntity;
import com.system.user.entityVO.UsageVO;
import com.system.user.enums.LightingPriceEnum;
import com.system.user.enums.WaterPriceEnum;
import com.system.user.service.*;
import com.system.user.weixin.WxPushServiceQcl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;

@Component
public class PayScheduledConfig {

    @Autowired
    private PayService payService;

    @Autowired
    private UsageService usageService;

    @Autowired
    private UserService userService;

    @Autowired
    private WaterService waterService;

    @Autowired
    private LightingService lightingService;

    public PayScheduledConfig() {
        payService = SpringUtils.getBean(PayService.class);
        usageService = SpringUtils.getBean(UsageService.class);
        userService = SpringUtils.getBean(UserService.class);
        waterService = SpringUtils.getBean(WaterService.class);
        lightingService = SpringUtils.getBean(LightingService.class);
    }

    @Scheduled(cron = "0 0 0 1 * ?")
    // cron内表达式意思（0秒0分0时1日每月每年），设定定时器用于记录每月水费费用
    @Async
    // 先查出上月的水量用量，并计算费用，再将相应数值和数据拼装好添加到支付表中，并启动微信小程序消息提醒推送
    public void newWaterPay() {
        List<UsageVO> usageVOS = usageService.findWaterByMonth();
        for (UsageVO u : usageVOS) {
            PayEntity pay = new PayEntity();
            double money = 0;
            if (u.getSumUse() >= 601) {
                money = u.getSumUse() * WaterPriceEnum.guangzhouMinThree.val();
            } else if (u.getSumUse() >= 261 && u.getSumUse() < 601) {
                money = u.getSumUse() * WaterPriceEnum.guangzhouMinTwo.val();
            } else {
                money = u.getSumUse() * WaterPriceEnum.guangzhouMinOne.val();
            }
            pay.setbNumber(u.getbNumber());
            pay.setMoney(money);
            payService.add(pay);
            List<String> openIds = userService.findOpenidByBNumber(u.getbNumber());
            /**
             * 小程序推送
             */
            WxPushServiceQcl wx = new WxPushServiceQcl();
            String site = waterService.findSite(u.getbNumber());
            for (String o : openIds) {
                String result = wx.pushPay(o, money, "水费", site);
                System.out.println(result);
            }
        }
    }

    @Scheduled(cron = "0 0 0 1 * ?")
    // cron内表达式意思（0秒0分0时1日每月每年），设定定时器用于记录每月电费费用
    @Async
    // 先查出上月的电量用量，并计算费用，再将相应数值和数据拼装好添加到支付表中，并启动微信小程序消息提醒推送
    public void newLightingPay() {
        List<UsageVO> usageVOS = usageService.findLightingByMonth();
        for (UsageVO u : usageVOS) {
            PayEntity pay = new PayEntity();
            double money = 0;
            if (u.getSumUse() > 34) {
                money = u.getSumUse() * LightingPriceEnum.guangzhouMinThree.val();
            } else if (u.getSumUse() > 26 && u.getSumUse() <= 34) {
                money = u.getSumUse() * LightingPriceEnum.guangzhouMinTwo.val();
            } else {
                money = u.getSumUse() * LightingPriceEnum.guangzhouMinOne.val();
            }
            pay.setbNumber(u.getbNumber());
            money = Double.parseDouble(String.format("%.2f", money));
            pay.setMoney(money);
            payService.add(pay);
            List<String> openIds = userService.findOpenidByBNumber(u.getbNumber());
            /**
             * 小程序推送
             */
            WxPushServiceQcl wx = new WxPushServiceQcl();
            String site = lightingService.findSite(u.getbNumber());
            for (String o : openIds) {
                String result = wx.pushPay(o, money, "电费", site);
                System.out.println(result);
            }
        }
    }

}
